package com.emp.test.springboottest.object;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.Assert;

/**
 * yhd.orders.get 获取订单列表 API 参数
 * @author empkh
 *
 */
public class YhdOrdersGetParam {
	
	/**日期类型(1：订单生成日期，2：订单付款日期，3：订单发货日期，4：订单收货日期，5：订单更新日期)*/
	public final static int DATE_TYPE_ORDER_CREATE = 1;
	public final static int DATE_TYPE_ORDER_PAY = 2;
	public final static int DATE_TYPE_ORDER_SEND = 3;
	public final static int DATE_TYPE_ORDER_RECEIVED = 4;
	public final static int DATE_TYPE_ORDER_UPDATE = 5;
	public final static int DATE_TYPE_DEFAULT = DATE_TYPE_ORDER_UPDATE;
	
	private final static int MAX_TIME_INTERVAL = 15; //unit minute
	private final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final int START_PAGE_DEFAULT = 1;
	private static final int PAGE_ROWS_DEFAULT = 50;
	
	
		/**订单状态（逗号分隔）: 
	ORDER_WAIT_PAY：已下单（货款未全收）、 
	ORDER_PAYED：已下单（货款已收）、 
	ORDER_WAIT_SEND：可以发货（已送仓库）、 
	ORDER_ON_SENDING：已出库（货在途）、 
	ORDER_RECEIVED：货物用户已收到、 
	ORDER_FINISH：订单完成、 
	ORDER_CANCEL：订单取消*/
	private String  orderStatusList; 
	
	private Integer  dateType; 
	/**查询开始时间*/
	private String  startTime; 
	/**查询结束时间(查询开始、结束时间跨度不能超过15天)*/
	private String  endTime; 
	/**当前页数*/
	private Integer  curPage; 
	/**每页显示记录数，默认50，最大100*/
	private Integer  pageRows;
	public String getOrderStatusList() {
		return orderStatusList;
	}
	public void setOrderStatusList(String orderStatusList) {
		this.orderStatusList = orderStatusList;
	}
	public Integer getDateType() {
		return dateType;
	}
	public void setDateType(Integer dateType) {
		this.dateType = dateType;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public Integer getCurPage() {
		return curPage;
	}
	public void setCurPage(Integer curPage) {
		this.curPage = curPage;
	}
	public Integer getPageRows() {
		return pageRows;
	}
	public void setPageRows(Integer pageRows) {
		this.pageRows = pageRows;
	}
	
	/**
	 * 获取结束时间，yhd有最长间隔时间为15天的限制
	 * @param startTime 
	 * @return
	 */
	public String getNextEndTime(String startTime){
		try {
			Date ds = DateUtils.parseDate(startTime,DATE_TIME_FORMAT);
			Date de = DateUtils.parseDate(endTime,DATE_TIME_FORMAT);
			Date dn = DateUtils.addDays(ds, MAX_TIME_INTERVAL);
			if(dn.before(de)){
				return DateFormatUtils.format(dn, DATE_TIME_FORMAT);
			}else{
				return endTime;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private YhdOrdersGetParam(String orderStatusList, Integer dateType, String startTime, String endTime,
			Integer curPage, Integer pageRows) {
		super();
		this.orderStatusList = orderStatusList;
		this.dateType = dateType;
		this.startTime = startTime;
		this.endTime = endTime;
		this.curPage = curPage;
		this.pageRows = pageRows;
	}
	
	public static final class Builder{
			private String  orderStatusList; 
			
			private Integer  dateType = DATE_TYPE_DEFAULT; 
			/**查询开始时间*/
			private String  startTime; 
			/**查询结束时间(查询开始、结束时间跨度不能超过15天)*/
			private String  endTime; 
			/**当前页数*/
			private Integer  curPage = START_PAGE_DEFAULT; 
			/**每页显示记录数，默认50，最大100*/
			private Integer  pageRows = PAGE_ROWS_DEFAULT;
			public String getOrderStatusList() {
				return orderStatusList;
			}
			public Builder setOrderStatusList(String orderStatusList) {
				this.orderStatusList = orderStatusList;
				return this;
			}
			public Integer getDateType() {
				return dateType;
			}
			public Builder setDateType(Integer dateType) {
				this.dateType = dateType;
				return this;
			}
			public String getStartTime() {
				return startTime;
			}
			public Builder setStartTime(String startTime) {
				this.startTime = startTime;
				return this;
			}
			public String getEndTime() {
				return endTime;
			}
			public Builder setEndTime(String endTime) {
				this.endTime = endTime;
				return this;
			}
			public Integer getCurPage() {
				return curPage;
			}
			public Builder setCurPage(Integer curPage) {
				this.curPage = curPage;
				return this;
			}
			public Integer getPageRows() {
				return pageRows;
			}
			public Builder setPageRows(Integer pageRows) {
				this.pageRows = pageRows;
				return this;
			}
			
			public YhdOrdersGetParam build(){
				
				Assert.notNull(this.getOrderStatusList(), "订单状态未填写");
				Assert.notNull(this.getStartTime(),"查询开始时间错误");
				Assert.notNull(this.getEndTime(),"查询结束时间错误");
				Assert.isTrue(this.getDateType() >= 1 && this.getDateType() <= 5, "时间类型错误");
				
				try {
					DateUtils.parseDate(startTime,DATE_TIME_FORMAT);
				} catch (ParseException e) {
					throw new IllegalArgumentException("开始时间格式错误");
				}
				try {
					DateUtils.parseDate(endTime,DATE_TIME_FORMAT);
				} catch (ParseException e) {
					throw new IllegalArgumentException("结束时间格式错误");
				}
				
				return new YhdOrdersGetParam(orderStatusList, dateType, startTime, endTime, curPage, pageRows);
			}
	}

	public static Builder getBuilder() {
		return new Builder();
	}
	
}
